package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.ParentsAdapter;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.GetParentListTask;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/7/27.
 */
public class ParentsActivity extends BaseActivity implements PullToRefreshListView.OnLoadDataListener, PullToRefreshListView.OnRefreshListener {
    private Context mContext;
    private final String TAG = getClass().getSimpleName();
    private PullToRefreshListView mListView;
    private ParentsAdapter mAdapter;
    private int currentPage = 1;
    private int size = 10;
    private int parentsTaskId = -1;
    private NoDataUtil mNoData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.parents_list);
        mContext = this;
        initView();
    }

    private void initView() {
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        View v = findViewById(R.id.content_layout);
        mNoData = new NoDataUtil(mContext,v);
        mListView = (PullToRefreshListView) findViewById(R.id.parents_list);
        mListView.init(mContext, true);
        mAdapter = new ParentsAdapter(mContext);
        mListView.setAdapter(mAdapter, true);
        mListView.setOnRefreshListener(this);
        mListView.setOnLoadDataListener(this);
        mListView.doRefresh();
    }

    private void getParentsList() {
        if (parentsTaskId != -1) {
            return;
        }
        if (currentPage == 0) {
            mListView.setNoNextPage();
            return;
        }

        GetParentListTask gtask = new GetParentListTask();
        gtask.addListener(parentListener);
        parentsTaskId = getEatShowApp().getTaskManager().addTask(gtask);

    }

    private TaskEventListener parentListener = new ShowToastTaskEventListener(this) {
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == parentsTaskId) {
                parentsTaskId = -1;
                if (currentPage == 1) {
                    mAdapter.clearData();
                    mListView.onRefreshComplete();
                    mListView.enableLoadMore();
                } else {
                    mListView.onLoadDataComplete(true);
                }
                List<EatShowUserInfo> fansList = new ArrayList<EatShowUserInfo>();
                if (data != null) {
                    fansList = (List<EatShowUserInfo>) data;
                }

                if (fansList != null && fansList.size() > 0) {
                    mAdapter.updateData(fansList);
                }

                if (mAdapter != null && mAdapter.getCount() ==0){
                    mNoData.ListViewEmpty(mListView,null,null,-1);
                }else {
                    mNoData.ListViewNoEmpty(mListView);
                }

                currentPage = result;


            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (taskId == parentsTaskId) {
                parentsTaskId = -1;
                if (currentPage == 1) {
                    mListView.onRefreshComplete();
                } else {
                    mListView.onLoadDataComplete(false);
                }
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    };

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getParentsList();
    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        getParentsList();
    }
}
